--
-- Created by IntelliJ IDEA.
-- Author: yao.chen
-- Date: 2018/12/12
-- GiftPanel.lua
-- 礼包直购（腾讯每日礼包）
--

local GiftRender = import(".renders.GiftRender")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GiftNode))
    sprite:init()
    return sprite
end

function ClassRef:init()
    self.super.init(self, data)

    local renders = {0, 0, 0}
    for i = 1, #renders do
        local render = GiftRender.create(self._ui.Panel:getChildByName("node_" .. i))
        render:onOperate(function(sender, data)
            self:_onClickRender(sender, data)
        end)
        render:setVisible(false)
        -- if i == 1 then
        --     render:setGiftType(Enums.GiftType.TaskGift)
        -- else
            render:setGiftType(Enums.GiftType.RMBGift)
        -- end
        renders[i] = render
    end
    self._gifts = renders
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:_onClickRender(render, data)
    local giftType = render:getGiftType()
    if giftType == Enums.GiftType.RMBGift then
        executeFunc(self._onOperate, "buyGift", data)
    elseif giftType == Enums.GiftType.TaskGift then
        executeFunc(self._onOperate, "getFreeDailyGift", data)
    end
end

function ClassRef:updateCoin(num)

end

function ClassRef:refreshPanel(data, isGiftPackage)
    local renders = self._gifts
    if not isGiftPackage then
        renders[1]:setGiftType(Enums.GiftType.TaskGift)
        if data.tasks and data.tasks[1] then
            renders[1]:setData(data.tasks[1])
            renders[1]:setVisible(true)
        end
    else
        for i = 1, #renders do
            if data[i] then
                renders[i]:setData(data[i])
                renders[i]:setVisible(true)
            else
                renders[i]:setVisible(false)
            end
        end
    end
end

return ClassRef

